package cn.icanci.loopstack.bic.web.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.UnitViewTreeService;
import cn.icanci.loopstack.bic.common.model.graph.UnitViewTreeVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.UnitViewTreeQueryForm;
import cn.icanci.loopstack.bic.web.mapper.UnitViewTreeWebMapper;
import cn.icanci.loopstack.bic.web.model.UnitViewTree;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/16 10:55
 */
@RestController
@RequestMapping("/bicAdmin/unitViewTree")
public class UnitViewTreeController {
    @Resource
    private UnitViewTreeWebMapper unitViewTreeWebMapper;
    @Resource
    private UnitViewTreeService   unitViewTreeService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody UnitViewTreeQueryForm form) {
        return R.builderOk()
            .data("queryPage",
                unitViewTreeService.queryPage(unitViewTreeWebMapper.web2vo(form.getUnitViewTree()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody UnitViewTree unitViewTree) {
        unitViewTreeService.save(unitViewTreeWebMapper.web2vo(unitViewTree));
        return R.builderOk().build();
    }

    @GetMapping("validateUnique/{systemUuid}/{useCaseUuid}")
    public R validateUnique(@PathVariable("systemUuid") String systemUuid, @PathVariable("useCaseUuid") String useCaseUuid) {
        UnitViewTreeVO unitViewTree = unitViewTreeService.queryBySystemUuidAndUseCaseUuid(systemUuid, useCaseUuid);
        return R.builderOk().data("result", unitViewTree == null).build();
    }
}
